output=[]
for _ in range(int(input())):
n,k=map(int,input().split())
s=input()
L=[]
i=1
t=0
count=1
while i<=len(s)-1:
if i==1 and s[i]=='W' and s[0]=='L':
L.append(0)
t=1
if s[i]=='W':
if i-t-1>0:
L.append(i-t-1)
t=i
if i>=1:
if s[i-1]=='W':
count+=2
elif s[i-1]=='L':
count+=1
i+=1
E=[]
if s=='W':
output.append('1')
elif 'W' not in s:
if n<=k:
output.append(str(1+2*(n-1)))
else:
if k>=1:
output.append(str(1+2*(k-1)))
elif k==0:
output.append(str(0))
else:
if s[-1]=='L':
E.append(n-1-t)
if s[0]=='L':
E.append(L.pop(0)+1)
count-=1
c=sum(L)+sum(E)
if k>=c:
output.append(str(2*n-1))
else:
L=sorted(L)
E=sorted(E)
m=0
for j in range(len(L)):
if L[j]<=k-m:
count+=2*(L[j]-1)+3
m+=L[j]
else:
count+=2*(k-m)
break
else:
for p in range(len(E)):
if E[p]<=k-m:
count+=2*E[p]
m+=E[p]
else:
count+=2*(k-m)
break
output.append(str(count))
print('\n'.join(output))
#include<bits/stdc++.h>
using namespace std;
// Macros definition
#define ll long long
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define ln cout<<"\n";
#define print(a) cout<< a << ' '
// Predefined values
ll mod = 1e9+7;
void solve(){
ll n,k;
cin>>n>>k;
string s;
cin>>s;
ll w=0;
for(char c : s){
w += (c=='W');
}
if(w==0){
k=min(k,n);
print(max(0ll,2*k-1));
ln;
return;
}
w += k;
if(w>=n){
print(2*n-1);
ln;
return;
}
vector<ll>v;
int l=0;
for(int i=0; i<n; i++){
if(s[i]=='L') l++;
else{
v.push_back(l);
l=0;
}
}
if(l>0) v.push_back(l);
if(s[0]=='L'){
v.erase(v.begin());
}
if(s.back()=='L') v.pop_back();
sort(v.begin(), v.end());
ll cnt=0, i=0;
while(i<v.size()){
if(cnt+v[i]>k) break;
cnt += v[i];
i++;
}
int x=v.size()-i;
print(2*w-1-x);
ln;
}
int main(){
// Deepu Yadav
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
287. Find the Duplicate Number | 279. Perfect Squares |
275. H-Index II | 274. H-Index |
260. Single Number III | 240. Search a 2D Matrix II |
238. Product of Array Except Self | 229. Majority Element II |
222. Count Complete Tree Nodes | 215. Kth Largest Element in an Array |
198. House Robber | 153. Find Minimum in Rotated Sorted Array |
150. Evaluate Reverse Polish Notation | 144. Binary Tree Preorder Traversal |
137. Single Number II | 130. Surrounded Regions |
129. Sum Root to Leaf Numbers | 120. Triangle |
102. Binary Tree Level Order Traversal | 96. Unique Binary Search Trees |
75. Sort Colors | 74. Search a 2D Matrix |
71. Simplify Path | 62. Unique Paths |
50. Pow(x, n) | 43. Multiply Strings |
34. Find First and Last Position of Element in Sorted Array | 33. Search in Rotated Sorted Array |
17. Letter Combinations of a Phone Number | 5. Longest Palindromic Substring |